﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_m_category : WebPageSecurity
{
    public static DataTable dt = null;
    public static string sortexpression = string.Empty;
    public static string sortdirection = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewDataGrid();
            CommonBLL.SetPermissionColumns(gv_ManageControl, Request.QueryString["mod"].ToString());
            LoadDataToDropDownList(DropDownListSearch, 0);
        }
    }
    private void LoadDataToDropDownList(DropDownList cboName, int CateID)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "--- Chọn danh mục cha ---";
        DataTable dtb = new CmsBLL().Category_GetTree(CateID);
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "_Title", "CateID");
        }
    }
    void ViewDataGrid()
    {
        dt = new CmsBLL().Category_GetTree(0, sortexpression, sortdirection);
        gv_ManageControl.DataSource = dt;
        gv_ManageControl.DataBind();
    }
    protected void gv_ManageControl_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_ManageControl.PageIndex = e.NewPageIndex;
        dt = new CmsBLL().Category_GetTree(0, sortexpression, sortdirection);
        gv_ManageControl.DataSource = dt;
        gv_ManageControl.DataBind();
    }
    public SortDirection SortDirection
    {
        get
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }
    protected void gv_ManageControl_Sorting(object sender, GridViewSortEventArgs e)
    {
        sortexpression = e.SortExpression;
        if (SortDirection == SortDirection.Ascending)
        {
            SortDirection = SortDirection.Descending;
            sortdirection = "DESC";
        }
        else
        {
            SortDirection = SortDirection.Ascending;
            sortdirection = "ASC";
        }
        dt = new CmsBLL().Category_GetTree(0, sortexpression, sortdirection);
        gv_ManageControl.DataSource = dt;
        gv_ManageControl.DataBind();
    }
    protected void btnEditItem_Click(object sender, EventArgs e)
    {
        int intNumCheck = 0;
        if (gv_ManageControl.Rows.Count > 0)
        {
            for (int i = 0; i < gv_ManageControl.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)gv_ManageControl.Rows[i].FindControl("chkCheck");
                HiddenField hdItemKeyFieldID = (HiddenField)gv_ManageControl.Rows[i].FindControl("hdItemKeyFieldID");
                if (chkChon.Checked == true)
                {
                    hdItemID.Value = hdItemKeyFieldID.Value + "";
                    intNumCheck++;
                }
            }
            if (intNumCheck > 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert1", "alert('Bạn chỉ được chọn một danh mục để sửa');", true);
            }
            else
            {
                if (intNumCheck == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert2", "alert('Bạn chưa chọn danh mục nào');", true);

                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), "KeyEditClick", "EditItem('e_category','EditItem','" + hdItemID.Value + "','560','300');", true);
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "EditItemKey", "<script type=\"text/javascript\">EditItem('e_leftmenu','" + hdItemID.Value + "','300px','100px','650','350')</script>");
                }
            }
        }
    }
    protected void btnDeleteItem_Click(object sender, EventArgs e)
    {
        if (gv_ManageControl.Rows.Count > 0)
        {
            List<int> listid = new List<int>();
            //List<int> retlistid = new List<int>();
            for (int i = 0; i < gv_ManageControl.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)gv_ManageControl.Rows[i].FindControl("chkCheck");
                HiddenField hdItemKeyFieldID = (HiddenField)gv_ManageControl.Rows[i].FindControl("hdItemKeyFieldID");

                if (chkChon.Checked == true)
                {
                    listid.Add(int.Parse(hdItemKeyFieldID.Value));
                }
            }
            if (listid.Count == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn cần chọn ít nhất một danh mục để xóa');", true);
            }
            else
            {
                for (int i = listid.Count - 1; i >= 0; i--)
                {
                    string reV = new BLL.CmsBLL().Category_DeleteItem(listid[i]);
                    if (reV.IndexOf("ERROR") != -1)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + reV + " bản ghi:" + listid[i].ToString() + "')", true);
                        break;
                    }
                }
            }
            ViewDataGrid();
        }
    }


    protected void lbtSearch_Click(object sender, EventArgs e)
    {
        dt = new BLL.CmsBLL().Category_Search(int.Parse("0" + DropDownListSearch.SelectedValue), txtKeyWord.Text + "");
        if (dt.Rows.Count > 0)
        {
            ltresultSearch.Text = "<font color=\"red\">Tìm thấy <b><u>" + dt.Rows.Count.ToString() + "</u></b> danh mục</font>";
            gv_ManageControl.DataSource = dt;
            gv_ManageControl.DataBind();
        }
        else
        {
            ltresultSearch.Text = "<font color=\"red\">Tìm thấy <b><u>" + dt.Rows.Count.ToString() + "</u></b> danh mục</font>";
            gv_ManageControl.DataSource = null;
            gv_ManageControl.DataBind();
        }
    }
}